//
//  ConvenientlyTakeListView.m
//  ConvenientlyTake
//
//  Created by 张奇 on 2022/12/21.
//

#import "ConvenientlyTakeListView.h"
#import "ConvenientlyTakeListCell.h"
#import "ConvenientlyTakeDetailVC.h"
#import "EventCenterModel.h"
#import "ConvenientlyTakeAPIRequest.h"

@interface ConvenientlyTakeListView ()<QMUITableViewDelegate, QMUITableViewDataSource, JXPagerMainTableViewGestureDelegate>

@property (nonatomic , assign) NSInteger page;
@property (nonatomic , strong) QMTableView *tableView;
@property (nonatomic , strong) NSMutableArray *dataArray;

@end

@implementation ConvenientlyTakeListView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self refreshData];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:KNotificationWithRefresheMainteanceReportListVC object:nil];
    }
    return self;
}

- (void)setupUI {
//    [self.tableView startPullDown];
    self.randomClapState = -1;
    self.tableView.gestureDelegate = self;
    [self refreshData];
}


-(void)refreshData {
    [self requestDataIsFirstPage:YES];
}
-(void)reloadTab {
    [self.tableView reloadData];
}
- (void)requestDataIsFirstPage:(BOOL)isFirstPage {
    if(isFirstPage){
        self.page = 1;
    }
  
    [TGProgressHUD showHUD:nil];
    [ConvenientlyTakeAPIRequest GETEventWithRandomClapState:self.randomClapState page:self.page success:^(TGResponseItem *responseItem) {
        [TGProgressHUD dismissMPHUD];
        if (responseItem.success) {
            self.page++;
            if (isFirstPage) {
                [self.dataArray removeAllObjects];
            }
            NSArray *list = [NSArray modelArrayWithClass:[EventCenterModel class] json:responseItem.data];
            [self.dataArray addObjectsFromArray:list];
            [self.tableView isShowFooter:list];
           
        }
        [self endRequestLoad];
    } fail:^(NSError *error) {
        [TGProgressHUD dismissMPHUD];
        [self endRequestLoad];
    }];
    
//    NSInteger count = arc4random()%5;
//    for (int i = 0; i< count; i++) {
//        [self.dataArray addObject:@""];
//    }
//    [self endRequestLoad];
}

- (void)endRequestLoad {
    [self.tableView reloadData];
    [self.tableView endPullUp];
    [self.tableView endPullDown];
}


#pragma mark ---------------
#pragma mark 数据源代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ConvenientlyTakeListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ConvenientlyTakeListCell"];
    cell.eventModel = [self.dataArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self endEditing:YES];
    ConvenientlyTakeDetailVC *detailVC = [[ConvenientlyTakeDetailVC alloc] init];
    EventCenterModel *model = [self.dataArray objectAtIndex:indexPath.row];
    detailVC.eventId = R(model.eventId);
    [self.getNavigationVC pushViewController:detailVC animated:YES];
}

- (QMTableView*)tableView {
    if (!_tableView) {
        QMTableView *tb = [[QMTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tb.delegate = self;
        tb.dataSource = self;
        tb.backgroundColor = UIColorClear;
        tb.estimatedRowHeight = RatioWidth(200);
        tb.separatorStyle = UITableViewCellSeparatorStyleNone;
        tb.showsVerticalScrollIndicator = NO;
        [tb registerNib:[UINib nibWithNibName:@"ConvenientlyTakeListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ConvenientlyTakeListCell"];
        tb.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        [self addSubview:tb];
        [tb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.right.mas_offset(0);
            make.bottom.mas_offset(0);
        }];
        tb.ly_emptyView = [MyEmptyView emptyWithContentViewDetailStr:@"暂时没有记录哦！" imageStr:@"empty_nodata" btnClickBlock:nil configerBlock:^(MyEmptyView *configer) {
            configer.contentViewOffset = -RatioHeight(50);
            configer.imageSize = CGSizeMake(RatioWidth(200), RatioWidth(200));
        }];
        WEAK
        [tb addLegendHeaderWithRefreshingBlock:^{
            STRONG
            [self requestDataIsFirstPage:YES];
        }];
        [tb addLegendFooterWithRefreshingBlock:^{
            STRONG
            [self requestDataIsFirstPage:NO];
        }];
        tb.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TableViewSpace)];
        [tb hideFooter];
        [tb startPullDown];
        
        _tableView = tb;
    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return NO;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
